package CRUD;

// default package
// Generated Apr 21, 2011 3:13:26 PM by Hibernate Tools 3.3.0.GA

import Utility.SessionFactoryUtil;
import java.util.List;
import javax.naming.InitialContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import xmlMapping.Forum;
import xmlMapping.Thread;

/**
 * Home object for domain model class Thread.
 * @see .Thread
 * @author Hibernate Tools
 */
public class ThreadHome {

	private static final Log log = LogFactory.getLog(ThreadHome.class);

      	public Session session;

    public ThreadHome() {
           if(session==null|| !session.isOpen())
           
        session=SessionFactoryUtil.getSessionFactory().openSession();
       
    }
 
	public void persist(Thread transientInstance) {
        
                session.beginTransaction();
            log.debug("persisting Thread instance");
		try {
			session.persist(transientInstance);
                         session.getTransaction().commit();
			log.debug("persist successful");
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
	}

	public void attachDirty(Thread instance) {
        
                session.beginTransaction();
		log.debug("attaching dirty Thread instance");
		try {
			session.saveOrUpdate(instance);
                         session.getTransaction().commit();
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Thread instance) {
        
                session.beginTransaction();
		log.debug("attaching clean Thread instance");
		try {
			session.lock(instance, LockMode.NONE);
                         session.getTransaction().commit();
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void delete(Thread persistentInstance) {
        
                session.beginTransaction();
		log.debug("deleting Thread instance");
		try {
			session.delete(persistentInstance);
                         session.getTransaction().commit();
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Thread merge(Thread detachedInstance) {
        
                session.beginTransaction();
		log.debug("merging Thread instance");
		try {
		Thread result = (Thread) session.merge(detachedInstance);
                 session.getTransaction().commit();
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public Thread findById(java.lang.Integer id) {
        
                session.beginTransaction();
		log.debug("getting Thread instance with id: " + id);
		try {
			Thread instance = (Thread) session.get(
					Thread.class, id);
			if (instance == null) {
				log.debug("get successful, no instance found");
			} else {
				log.debug("get successful, instance found");
			}
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Thread instance) {
        
                session.beginTransaction();
		log.debug("finding Thread instance by example");
		try {
			List results = session.createCriteria(
					Thread.class).add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

        public List getSticky(Forum forum){
        
                session.beginTransaction();
             Criteria crit=  session.createCriteria(Thread.class);
            Criterion forumId=Restrictions.eq("forum", forum);
            Criterion stickybool=Restrictions.eq("sticky", true);
            Criterion cond=Restrictions.and(forumId, stickybool);
            crit.add(cond);
            return crit.list();
        }
        public List getThreads(Forum forum){
        
                session.beginTransaction();
            Criteria crit=  session.createCriteria(Thread.class);
            Criterion catId=Restrictions.eq("forum", forum);
            crit.add(catId);
            return crit.list();
        }
        public List search(String key){
        
                session.beginTransaction();
            Criteria crit=  session.createCriteria(Thread.class);
            Criterion title=Restrictions.like("title", key,MatchMode.ANYWHERE);
            crit.add(title);
            return crit.list();
        }
}
